<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Models\AttendanceStatistics;
use Modules\Manager\Entities\Manager;
use App\Http\Controllers\Wx\SendTemplateController;
use Modules\Car\Entities\CarOrder;

class SqcChaoshi implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $order;
    public function __construct()
    {
        //
       // $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $modeldata = AttendanceStatistics::query()->whereIn('type', [7])->where('operator_id', '!=',0)->where('is_send_wxmsg', 1)->where('sd_time','')->where('day','<',now()->subMinutes(30))->get();
        $modeldata2 = AttendanceStatistics::query()->whereIn('type', [6])->where('operator_id', '!=',0)->where('is_send_wxmsg_qh', 1)->where('qh_time','')->where('day','<',now()->subMinutes(30))->get();
        if(count($modeldata)>0){
            foreach ($modeldata as $item){
                // day接单时间超过30分钟的，发送提醒并记录已发送过提醒
                $useritem = Manager::query()->where('id', $item->operator_id)->first();
                if($useritem&&$useritem->wechat_openid){
                    $newTime = strtotime('+30 minutes', strtotime($item->day));
                    $orderinfo = CarOrder::query()->where('order_sn',$item->order_sn)->first();
                    $msg = [
                        'openid'=>$useritem->wechat_openid,
                        'plate_number'=>$item->plate_number??$orderinfo->plate_number,
                        'msg'=>'送车交接工单',
                        'name'=>$orderinfo->wx_name,
                        'time'=>date('Y-m-d H:i:s', $newTime),
                        'page'=>'pages/admin/sqc/sqc', // 发布的时候加上
                    ];
                    (new SendTemplateController())->sendTemplateSccs($msg);
                    info(json_encode($msg));
                }
                AttendanceStatistics::query()->where('id', $item->id)->update(['is_send_wxmsg'=>2]);
            }
        }
        if(count($modeldata2)>0){
            foreach ($modeldata2 as $item){
                // day接单时间超过30分钟的，发送提醒并记录已发送过提醒
                $useritem = Manager::query()->where('id', $item->operator_id)->first();
                if($useritem&&$useritem->wechat_openid){
                    $newTime = strtotime('+30 minutes', strtotime($item->day));
                    $orderinfo = CarOrder::query()->where('order_sn',$item->order_sn)->first();
                    $msg = [
                        'openid'=>$useritem->wechat_openid,
                        'plate_number'=>$item->plate_number??$orderinfo->plate_number,
                        'msg'=>'取车交接工单',
                        'name'=>$orderinfo->wx_name,
                        'time'=>date('Y-m-d H:i:s', $newTime),
                        'page'=>'pages/admin/sqc/sqc', // 发布的时候加上
                    ];
                    (new SendTemplateController())->sendTemplateSccs($msg);
                    info(json_encode($msg));
                }
                AttendanceStatistics::query()->where('id', $item->id)->update(['is_send_wxmsg_qh'=>2]);
            }
        }
    }
}
